package com.gitee.fanx.rpc.test.server;

import com.gitee.fanx.rpc.RpcException;
import com.gitee.fanx.rpc.api.DemoApi;
import com.gitee.fanx.rpc.dto.*;
import com.gitee.fanx.rpc.server.RpcExport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author fanhang
 */
@RpcExport(DemoApi.class)
public class DemoImpl implements DemoApi, InitializingBean {
	private static final Logger log = LoggerFactory.getLogger(DemoImpl.class);

	private final TreeSet<UserDto> users = new TreeSet<>();

	@Override
	public String testNoArgs() {
		return null;
	}

	@Override
	public Map<String, Object> sayHello(String name) {
		if (name.length() >= 5) {
			throw new RpcException(400, "错参数", "");
		}
		return Map.of("hello", name);
	}

	@Override
	public String doSth(String aa, String bb) {
		return String.format("hey: %s, %s", aa, bb);
	}

	@Override
	public String doSth(String aa, List<UserDto> bb) {
		return aa + "#" + bb.stream().map(UserDto::getName).collect(Collectors.joining(","));
	}

	private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HHmmss");

	@Override
	public UserResultDto hello(UserDto user) {
		UserResultDto result = new UserResultDto();
		result.setId(user.getId());
		result.setName(user.getName());
		result.setRemark("rem:" + dateFormat.format(new Date()));
		return result;
	}

	@Override
	public PageResponse<UserDto> getList(PageRequest pageRequest) {
		log.info("request: {}", pageRequest);
		int startIndex = pageRequest.getPageNum() * pageRequest.getPageSize();
		List<UserDto> list = users.stream().skip(startIndex).limit(pageRequest.getPageSize()).toList();
		int pages = users.size() / pageRequest.getPageSize();
		if (users.size() % pageRequest.getPageSize() != 0) {
			pages = pages + 1;
		}
		return new PageResponse<>(pageRequest.getPageNum(), pageRequest.getPageSize(), users.size(), pages, list);
	}

	@Override
	public TestGeneric<TestGeneric<UserDto>> testGeneric(String aa, TestGeneric<TestGeneric<TestGeneric<TestGeneric<String>>>> data) {
		log.info("aa: {}, data: {}", aa, data);
		String innerStr = data.getResult().getResult().getResult().getResult();
		int id = Math.abs(innerStr.hashCode() % 10);
		UserDto user = new UserDto();
		user.setId(id);
		user.setName("name-" + innerStr);
		user.setAge(id % 3 * 7 + 8);
		TestGeneric<UserDto> tgInner = new TestGeneric<>(2);
		tgInner.setResult(user);
		TestGeneric<TestGeneric<UserDto>> tg = new TestGeneric<>(1);
		tg.setResult(tgInner);
		return tg;
	}

	@Override
	public TestResult testResultInnerGeneric(String aa, List<String> list) {
		TestGeneric<String> tg = new TestGeneric<>(1);
		tg.setResult(String.join("#", list));
		TestGeneric<TestGeneric<String>> result = new TestGeneric<>(3);
		result.setResult(tg);
		TestResult testResult = new TestResult();
		testResult.setResult(result);
		return testResult;
	}

	@Override
	public void testReturnVoid(String name) {
		System.out.println("testReturnVoid: " + name);
	}

	private static UserDto createUser(int i) {
		UserDto user = new UserDto();
		user.setId(i);
		user.setName("name-" + i);
		int age = i % 10 + 10;
		user.setAge(age);
		return user;
	}

	@Override
	public void afterPropertiesSet() {
		IntStream.range(1, 77).forEach(x -> {
			users.add(createUser(x));
		});
	}
}
